﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CaiBaby.Common;
using CaiBaby.Entity;

namespace CaiBaby.DAL
{
	/// <summary>
    /// 管理员数据数据访问层
	/// </summary>
	public class Manger
	{
		/// <summary>
		/// 增加一个管理员
		/// </summary>
		public int Add(MangerInfo mangerInfo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("INSERT INTO ma_Manger(");
			strSql.Append("RId,LoginName,PassWord,NickName,CreateTime,State)");
			strSql.Append(" VALUES (");
			strSql.Append("@RId,@LoginName,@PassWord,@NickName,@CreateTime,@State)");
			strSql.Append(";SELECT @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@RId", SqlDbType.Int,4),
					new SqlParameter("@LoginName", SqlDbType.VarChar,32),
					new SqlParameter("@PassWord", SqlDbType.VarChar,32),
					new SqlParameter("@NickName", SqlDbType.VarChar,32),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.Int,4)};
            parameters[0].Value = mangerInfo.RId;
            parameters[1].Value = mangerInfo.LoginName;
            parameters[2].Value = mangerInfo.PassWord;
            parameters[3].Value = mangerInfo.NickName;
            parameters[4].Value = mangerInfo.CreateTime;
            parameters[5].Value = mangerInfo.State;

			object obj = SQLHelper.GetSingle(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
        /// 删除一个管理员
		/// </summary>
		/// <param name="mId">管理员ID</param>
        /// <returns>返回是否成功</returns>
		public bool Delete(int mId)
		{
            string strSql = "DELETE FROM ma_Manger WHERE MId=@MId";
			SqlParameter[] parameters = {
					new SqlParameter("@MId", SqlDbType.Int,4)};
            parameters[0].Value = mId;
			int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql,parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 修改管理员昵称
        /// </summary>
        /// <param name="mId">管理员ID</param>
        /// <param name="nickName">新昵称</param>
        /// <returns></returns>
        public bool UpdateNickName(int mId,string nickName)
        {
            string strSql = "UPDATE ma_Manger SET NickName = @NickName WHERE MId=@MId";
            SqlParameter[] parameters = {
					new SqlParameter("@MId", SqlDbType.Int,4),
                    new SqlParameter("@NickName", SqlDbType.VarChar,32)};
            parameters[0].Value = mId;
            parameters[1].Value = nickName;
            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="mId">管理员ID</param>
        /// <param name="newPassWord">新密码</param>
        /// <returns></returns>
        public bool UpdatePassword(int mId, string newPassWord)
        {
            string strSql = "UPDATE ma_Manger SET PassWord = @PassWord WHERE MId=@MId";
            SqlParameter[] parameters = {
					new SqlParameter("@MId", SqlDbType.Int,4),
                    new SqlParameter("@PassWord", SqlDbType.VarChar,32)};
            parameters[0].Value = mId;
            parameters[1].Value = newPassWord;
            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="loginName">登录帐号</param>
        /// <param name="passWord">密码</param>
        /// <param name="msg">返回的错误消息</param>
        /// <returns>返回是否成功，不成功查看msg</returns>
        public MangerInfo Login(string loginName, string passWord, out string msg)
        {
            string strSql = "SELECT TOP 1 * FROM ma_Manger WHERE LoginName = @LoginName";
            SqlParameter[] parameters = {
					new SqlParameter("@LoginName", SqlDbType.VarChar,32)
			};
			parameters[0].Value = loginName;
            MangerInfo mangerInfo = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql, parameters))
            {
                mangerInfo = new MangerInfo();
                mangerInfo.MId = int.Parse(dr["MId"].ToString());
                mangerInfo.RId = int.Parse(dr["RId"].ToString());
                mangerInfo.LoginName = dr["LoginName"].ToString();
                mangerInfo.PassWord = dr["PassWord"].ToString();
                mangerInfo.NickName = dr["NickName"].ToString();
                mangerInfo.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                mangerInfo.LastlogTime = DateTime.Parse(dr["LastlogTime"].ToString());
                mangerInfo.LastlogIp = dr["LastlogIp"].ToString();
                mangerInfo.State = int.Parse(dr["State"].ToString());
            }
            if (mangerInfo == null)
            {
                msg = "loginname";
                return null;
            }
            if (mangerInfo.PassWord != passWord)
            {
                msg = "password";
                return null;
            }
            msg = string.Empty;
            return mangerInfo;
        }

        /// <summary>
        /// 更新最后登录信息
        /// </summary>
        /// <param name="mId">管理员ID</param>
        /// <returns>返回是否成功</returns>
        public bool UpDataLastLog(int mId,string ip)
        {
            string strSql = "UPDATE ma_Manger SET LastlogTime = @LastlogTime , LastlogIp = @LastlogIp WHERE MId=@MId";
            SqlParameter[] parameters = {
					new SqlParameter("@LastlogTime", SqlDbType.DateTime),
                    new SqlParameter("@LastlogIp", SqlDbType.VarChar,20),
                    new SqlParameter("@MId", SqlDbType.Int,4)
			};
            parameters[0].Value = DateTime.Now;
            parameters[1].Value = ip;
            parameters[2].Value = mId;
            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
        /// 获得管理员列表
		/// </summary>
		/// <param name="strWhere">条件 如 1=1 and 2=2</param>
		/// <returns></returns>
		public DataTable GetList(string strWhere)
		{
            string strSql = "SELECT * FROM ma_Manger";
			if(strWhere.Trim()!="")
			{
				strSql += " where "+strWhere;
			}
			return SQLHelper.ExecuteDataSet(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString()).Tables[0];
		}
	}
}

